package com.situ.company.department.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.company.department.dao.IDepartmentDao;
import com.situ.company.department.model.DepartmentModel;
import com.situ.company.department.service.IDepartmentService;
import com.situ.company.department.service.impl.DepartmentServiceImpl;
import com.situ.company.util.FmtRequest;

@WebServlet("/department")
public class DepartmentServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action=req.getParameter("action");
		Object res="";
		switch (action) {
		case "list":
			res=list(req);
			break;
		case "page":
			res=page(req);
			break;
		case "add":
			res=add(req);
			break;
		case "del":
			res=del(req);
			break;
		case "get":
			res=get(req);
			break;
		case "upd":
			res=upd(req);
			break;
		default:
			break;
		}
		FmtRequest.write(resp.getWriter(), res);
	}
	private Object page(HttpServletRequest req) {
		Map<String,String> fields=new HashMap<>(); 
		fields.put("name", "name");
		fields.put("code", "code");
		DepartmentModel model=FmtRequest.parseModel(req, DepartmentModel.class,fields);
		String pageIndex=req.getParameter("pageIndex");
		String pageLimit=req.getParameter("pageLimit");
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		List<DepartmentModel> list = service.selectList(model);
		//测试
//		for (int i = 0; i < list.size(); i++) {
//			System.out.println(list.get(i));
//		}
		
		int count=service.selectCount(model);
		//返回给网页用map整合
		Map<String,Object> map=new HashMap<>();
		map.put("list", list);
		map.put("count",count);
		return map;
	}
	private Object upd(HttpServletRequest req) {
		DepartmentModel model=FmtRequest.parseModel(req, DepartmentModel.class);
		return service.update(model);
	}
	private Object get(HttpServletRequest req) {
		System.out.println(req.getParameter("code"));
		DepartmentModel model=new DepartmentModel(req.getParameter("code"));
		return service.selectModel(model);
	}
	private Object del(HttpServletRequest req) {
		DepartmentModel model=FmtRequest.parseModel(req, DepartmentModel.class);
		return service.delete(model);
	}
	private Object add(HttpServletRequest req) {
		DepartmentModel model=FmtRequest.parseModel(req, DepartmentModel.class);
		return service.insert(model);
	}
	
	private IDepartmentService service=new DepartmentServiceImpl();
	private List<DepartmentModel> list(HttpServletRequest req) {
		DepartmentModel model=FmtRequest.parseModel(req, DepartmentModel.class);
		return service.selectList(model);
	}
}
